﻿@using FujiyBlog.Web.Models
@using HtmlAgilityPack
@model FujiyBlog.Core.Dto.PostSummary

@functions {

    private string ShowPostContent(string content)
    {
        content = content ?? string.Empty;
        int moreIndex = content.IndexOf("[more]", StringComparison.OrdinalIgnoreCase);

        if (Model.ShowFullPost)
        {
            if (moreIndex >= 0)
            {
                content = content.Remove(moreIndex, 6);
                content = content.Insert(moreIndex, "<span id=\"continue\"></span>");
            }
        }
        else
        {
            if (moreIndex >= 0)
            {
                content = content.Substring(0, content.IndexOf("[more]", StringComparison.OrdinalIgnoreCase));
                return CloseOpenedTags(content) + " " + Html.ActionLink(Resources.Labels.ReadMore, MVC.Post.Details(Model.Post.Slug), null, null, null, "continue");
            }
        }
        return CloseOpenedTags(content);
    }

    private string CloseOpenedTags(string html)
    {
        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(html);
        StringWriter stringWriter = new StringWriter();
        doc.Save(stringWriter);
        return stringWriter.ToString();
    }

}

<div class="post" id="post@(Model.Post.Id)">
    <h2 class="title">
        @Html.ActionLink(Model.Post.Title, MVC.Post.Details(Model.Post.Slug))
    </h2>

    <p class="meta">
        <span class="date">@FujiyBlog.Web.Common.DateTimeUtil.ConvertUtcToMyTimeZone(Model.Post.PublicationDate).ToString(System.Globalization.CultureInfo.CreateSpecificCulture(UICulture))</span><span class="posted">
            @Resources.Labels.By @Html.ActionLink(Model.Post.Author.DisplayName ?? Model.Post.Author.Username, MVC.Post.Author(Model.Post.Author.Username, null))
        </span>
    </p>

    <div class="entry">
        @Html.Raw(ShowPostContent(Model.Post.Content))
        <div class="post-footer">
            <div class="social-networks">
                @if (Settings.SettingRepository.EnableTwitterSharePosts)
                {
                    <a href="javascript:void(0);" onclick="fujiyBlog.windowOpenCenter('http://twitter.com/intent/tweet?url=@Uri.EscapeDataString(Url.Action(MVC.Post.Details(Model.Post.Slug), Request.Url.Scheme))&text=@Uri.EscapeDataString(Model.Post.Title)&via=@Uri.EscapeDataString(Settings.SettingRepository.TwitterBlogAccount ?? "")', '', 550, 420)"><img src="~/Content/Images/tweet.png" /></a>
                }
                @if (Settings.SettingRepository.EnableGooglePlusOnePosts)
                {
                    <a href="javascript:void(0);" onclick="fujiyBlog.windowOpenCenter('https://plus.google.com/share?url=@Uri.EscapeDataString(Url.Action(MVC.Post.Details(Model.Post.Slug), Request.Url.Scheme))', '', 510, 420)"><img src="~/Content/Images/googleplus.png" /></a>
                }
                @if (Settings.SettingRepository.EnableFacebookLikePosts)
                {
                    @Facebook.Share(Url.Action(MVC.Post.Details(Model.Post.Slug), Request.Url.Scheme))
                }
            </div>

            <div class="post-links">
                @if (Request.IsAuthenticated)
                {
                    @Html.ActionLink(Resources.Labels.Edit, MVC.Admin.Post.Edit(Model.Post.Id)) <text>|</text>
                    @Ajax.ActionLink(Resources.Labels.Delete, MVC.Admin.Post.Delete((Model.Post.Id)), new AjaxOptions { HttpMethod = "POST", Confirm = "Sure?", OnSuccess = "window.location.href='" + Url.Content("~/") + "';" }) <text>|</text>
                }
                @Html.ActionLink("Permalink", MVC.Post.DetailsById(Model.Post.Id), new { rel = "bookmark", title = Model.Post.Title })
                | <a href="@Url.Action(MVC.Post.Details(Model.Post.Slug))#commentlist">@Resources.Labels.Comments (@Model.CommentsTotal)</a>
            </div>

            <div class="bottom">
                @if (@Model.Post.Tags.Any())
                {
                    <div>
                        Tags: @Html.Raw(Model.Post.Tags.Select(x => x.Name).Aggregate("", (x, y) => x + Html.ActionLink(y, MVC.Post.Tag(y, null)) + ", ", x => x.Trim(',', ' ')))
                    </div>
                }
                @if (@Model.Post.Categories.Any())
                {
                    <div>
                        @Resources.Labels.Categories: @Html.Raw(Model.Post.Categories.Select(x => x.Name).Aggregate("", (x, y) => x + Html.ActionLink(y, MVC.Post.Category(y, null)) + ", ", x => x.Trim(',', ' ')))
                    </div>
                }
            </div>
        </div>
    </div>
</div>
